package haizhi.fruitmall.service.impl;

import com.github.pagehelper.PageHelper;
import haizhi.fruitmall.dao.mapper.PictureMapper;
import haizhi.fruitmall.enumeration.PictureEnum;
import haizhi.fruitmall.exception.ResultException;
import haizhi.fruitmall.pojo.form.PictureForm;
import haizhi.fruitmall.pojo.po.Picture;
import haizhi.fruitmall.pojo.po.PictureExample;
import haizhi.fruitmall.pojo.po.PictureType;
import haizhi.fruitmall.service.PictureService;
import haizhi.fruitmall.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * Created by richa on 2017/9/13
 */

@Slf4j
@Service
@Transactional
public class PictureServiceImpl implements PictureService{

    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public void insert(PictureForm form) {
        Picture record = new Picture();
        BeanUtil.copyBean(form,record);
        record.setPictureId(BeanUtil.longId());
        pictureMapper.insertSelective(record);
    }

    @Override
    public void update(PictureForm form){
        Picture record = this.getone(form.getPictureId());
        if(BeanUtil.isNull(record)){ throw new ResultException("记录不存在！"); }
        BeanUtil.copyBean(form,record);
        record.setUpdateTime(BeanUtil.newTime());
        pictureMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void delete(Long id) {
        if(BeanUtil.isNull(this.getone(id))){ throw new ResultException("记录不存在！"); }
        pictureMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Picture getone(Long id) {
        if(BeanUtil.isNull(id)){ throw new ResultException("编号为空！");}
        return pictureMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Picture> getall(PictureForm form) {
        PictureExample example = new PictureExample();
        PictureExample.Criteria criteria = example.createCriteria();
        if(BeanUtil.noNull(form.getPictureType())){
            criteria.andPictureTypeEqualTo(form.getPictureType());
        }
        if(BeanUtil.noNull(form.getPageNum()) && BeanUtil.noNull(form.getPageSize())){
            PageHelper.startPage(form.getPageNum(),form.getPageSize());
        }
        List<Picture> pictureList = pictureMapper.selectByExample(example);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        for(Picture picture : pictureList){
            if(BeanUtil.noEmpty(picture.getPicturePath())){
                String picturePath = BeanUtil.getPath(request)+picture.getPicturePath();
                picture.setPicturePath(picturePath);
            }
        }
        return pictureMapper.selectByExample(example);
    }

    @Override
    public List<PictureType> getType(){
        return Arrays.asList(new PictureType(PictureEnum.SLIDED_PICTURE),new PictureType(PictureEnum.STATIC_PICTURE));
    }
}
